package com.css.fxfzaqbz.modules.assess2.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.UTask;
import com.css.fxfzaqbz.modules.assess2.repository.UTaskRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class UTaskRepositoryImpl implements UTaskRepository {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void saveTask(UTask uTask) {
        String sql = "insert into sys_u_task (ut_id,ut_activeid,ut_status,ut_create_time," +
                "ut_create_user,ut_del_flag,ut_opinion,content) values(:utId,:utActiveid,:utStatus,:utCreateTime," +
                ":utCreateUser,:utDelFlag,:utOpinion,:content)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(uTask);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public UTask getTask(String id) {
        try {
            String sql="select * from sys_u_task where ut_activeid='"+id+"' and ut_del_flag='"+ YNEnum.N+"'";
            UTask uTask = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UTask.class));
            return uTask;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_u_task set" +
                " ut_status = '"+YNEnum.Y+"' " +
                " where ut_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_u_task set" +
                " ut_status = '"+2+"' " +
                " where ut_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
